package cn.fl.product.controller;

import cmm.mid.core.framework.page.DataResultVO;
import cmm.mid.core.framework.page.ListPageVO;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.logger.eum.LogOperEnum;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.DateUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.capital.constant.CapPrjArInfoConstant;
import cn.fl.capital.transform.CapPrjArInfoVOTransform;
import cn.fl.capital.vo.CapPrjArInfoVO;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.product.service.ISplCapPrjArInfoService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import fl.constant.facade.framework.constant.BusinessIdConstant;
import fl.mongo.facade.capital.dto.CapPriArInfoDTO;
import fl.spl.facade.framework.constant.SplSystemConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description: 供应商信息表 控制层
 */
@Controller
@RequestMapping("/splCap/capPrjAr")
public class SplCapPrjArInfoController {

    @Autowired
    private ISplCapPrjArInfoService splCapPrjArInfoService ;

    @RequestMapping(value = "/toCapPrjArListPage.do", method = { RequestMethod.GET,RequestMethod.POST})
    public ModelAndView toListPage(HttpServletRequest request, HttpServletResponse response) {
        String dataId = request.getParameter("dataId");
        String type = request.getParameter("type");
        ModelAndView view = new ModelAndView("project/capPrjAr/splCstPrjCapArInfo");
        view.addObject("dataId",dataId);
        view.addObject("type",type);

        return view ;
    }

    @RequestMapping(value = "/getCapPrjArInfo.do", method = { RequestMethod.GET,RequestMethod.POST})
    @ResponseBody
    public Object getCapPrjArInfo(HttpServletRequest request, HttpServletResponse response) {
        //DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES,"查询成功");
        String dataId = request.getParameter("dataId");//数据ID
        String type = request.getParameter("type");//
        //CapPrjArInfoConstant
        if(StringUtils.isEmpty(dataId)){
            /*result.setInfo("业务ID为空，查询失败");
            result.setSuccess(BaseConstant.IS_YESNO_NO);*/
            BaseLogUtils.newWebLogger("数据ID为空，查询失败！")
                    .setSysCd(CurrentThreadContext.getCurrentSysCd())
                    .setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
                    .setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
                    .setWarning(false)
                    .info();
            return new ListPageVO<>() ;
        }
        if(StringUtils.isEmpty(type)){
            BaseLogUtils.newWebLogger("数据类型为空，查询失败！")
                    .setSysCd(CurrentThreadContext.getCurrentSysCd())
                    .setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
                    .setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
                    .setWarning(false)
                    .info();
            return new ListPageVO<>() ;
        }
        List<Long> ids = new ArrayList<>();
        ids.add(Long.valueOf(dataId));
        List<CapPriArInfoDTO> dtoList = splCapPrjArInfoService.getCapPrjArInfoDTOByDataIds(ids,type, DateUtils.formatDate(new Date(),DateUtils.YYYY_MM_DD));
        List<CapPrjArInfoVO> voList = new ArrayList();
        if(CollectionUtils.isNotEmpty(dtoList)){
            voList = CapPrjArInfoVOTransform.mongoDtoVOList(dtoList);
            voList = resolveCapPrjArInfo(voList);
        }
        ListPageVO<CapPrjArInfoVO> listPage = new ListPageVO<CapPrjArInfoVO>(voList,voList.size());
        return listPage ;
    }

    public  List<CapPrjArInfoVO>  resolveCapPrjArInfo(List<CapPrjArInfoVO> voList){
        List<CapPrjArInfoVO> list = new ArrayList<CapPrjArInfoVO>();
        for(CapPrjArInfoVO object : voList){
            //融资新车
            CapPrjArInfoVO rzNewCar = new CapPrjArInfoVO();
            rzNewCar.setRecordType(0);
            rzNewCar.setRzOrDb(0);
            rzNewCar.setCarType(1);
            rzNewCar.setId(object.getId());
            rzNewCar.setCrtUsrId(object.getCrtUsrId());
            rzNewCar.setPrjId(object.getPrjId());//项目ID
            rzNewCar.setMainId(object.getMainId());//主体ID(承租人/担保人/挂靠公司/供应商ID)
            rzNewCar.setMainNm(object.getMainNm());//主体名称(承租人/担保人/挂靠公司/供应商名称)
            rzNewCar.setMainTypCd(object.getMainTypCd());//主体类型(CAP0053)：1.承租人 2.担保人 3.挂靠公司 4.供应商
            rzNewCar.setFlNewOvdHisCnt(object.getFlNewOvdHisCnt());//融资新车-历史30+逾期次数
            rzNewCar.setFlNewOvdCnt(object.getFlNewOvdCnt());//融资新车-当前逾期支付表数
            rzNewCar.setFlNewPrjAudAmt(object.getFlNewPrjAudAmt());//融资新车-已发起项目评审未通过的支付表敞口
            rzNewCar.setFlNewPayAmt(object.getFlNewPayAmt());//融资新车-未放款支付表敞口
            rzNewCar.setFlNewPaidAmt(object.getFlNewPaidAmt());//融资新车-已放款支付表敞口

            //融资二手车
            CapPrjArInfoVO rzOldCar = new CapPrjArInfoVO();
            rzOldCar.setRecordType(0);
            rzOldCar.setRzOrDb(0);
            rzOldCar.setCarType(0);
            rzOldCar.setId(object.getId());
            rzOldCar.setCrtUsrId(object.getCrtUsrId());
            rzOldCar.setPrjId(object.getPrjId());//项目ID
            rzOldCar.setMainId(object.getMainId());//主体ID(承租人/担保人/挂靠公司/供应商ID)
            rzOldCar.setMainNm(object.getMainNm());//主体名称(承租人/担保人/挂靠公司/供应商名称)
            rzOldCar.setMainTypCd(object.getMainTypCd());//主体类型(CAP0053)：1.承租人 2.担保人 3.挂靠公司 4.供应商
            rzOldCar.setFlUsedOvdHisCnt(object.getFlUsedOvdHisCnt());//融资二手车-历史30+逾期次数
            rzOldCar.setFlUsedOvdCnt(object.getFlUsedOvdCnt());//融资二手车-当前逾期支付表数
            rzOldCar.setFlUsedPrjAudAmt(object.getFlUsedPrjAudAmt());//融资二手车-已发起项目评审未通过的支付表敞口
            rzOldCar.setFlUsedPayAmt(object.getFlUsedPayAmt());//融资二手车-未放款支付表敞口
            rzOldCar.setFlUsedPaidAmt(object.getFlUsedPaidAmt());//融资二手车-已放款支付表敞口

            //担保新车
            CapPrjArInfoVO dbNewCar = new CapPrjArInfoVO();
            dbNewCar.setRecordType(0);
            dbNewCar.setRzOrDb(1);
            dbNewCar.setCarType(1);
            dbNewCar.setId(object.getId());
            dbNewCar.setCrtUsrId(object.getCrtUsrId());
            dbNewCar.setPrjId(object.getPrjId());//项目ID
            dbNewCar.setMainId(object.getMainId());//主体ID(承租人/担保人/挂靠公司/供应商ID)
            dbNewCar.setMainNm(object.getMainNm());//主体名称(承租人/担保人/挂靠公司/供应商名称)
            dbNewCar.setMainTypCd(object.getMainTypCd());//主体类型(CAP0053)：1.承租人 2.担保人 3.挂靠公司 4.供应商
            dbNewCar.setGtNewOvdHisCnt(object.getGtNewOvdHisCnt());//担保新车-历史30+逾期次数
            dbNewCar.setGtNewOvdCnt(object.getGtNewOvdCnt());//担保新车-当前逾期支付表数
            dbNewCar.setGtNewPrjAudAmt(object.getGtNewPrjAudAmt());//担保新车-已发起项目评审未通过的支付表敞口
            dbNewCar.setGtNewPayAmt(object.getGtNewPayAmt());//担保新车-未放款支付表敞口
            dbNewCar.setGtNewPaidAmt(object.getGtNewPaidAmt());//担保新车-已放款支付表敞口

            //担保二手车
            CapPrjArInfoVO dbOldCar = new CapPrjArInfoVO();
            dbOldCar.setRecordType(0);
            dbOldCar.setRzOrDb(1);
            dbOldCar.setCarType(0);
            dbOldCar.setId(object.getId());
            dbOldCar.setCrtUsrId(object.getCrtUsrId());
            dbOldCar.setPrjId(object.getPrjId());//项目ID
            dbOldCar.setMainId(object.getMainId());//主体ID(承租人/担保人/挂靠公司/供应商ID)
            dbOldCar.setMainNm(object.getMainNm());//主体名称(承租人/担保人/挂靠公司/供应商名称)
            dbOldCar.setMainTypCd(object.getMainTypCd());//主体类型(CAP0053)：1.承租人 2.担保人 3.挂靠公司 4.供应商
            dbOldCar.setGtUsedOvdHisCnt(object.getGtUsedOvdHisCnt());//担保二手车-历史30+逾期次数
            dbOldCar.setGtUsedOvdCnt(object.getGtUsedOvdCnt());//担保二手车-当前逾期支付表数
            dbOldCar.setGtUsedPrjAudAmt(object.getGtUsedPrjAudAmt());//担保二手车-已发起项目评审未通过的支付表敞口
            dbOldCar.setGtUsedPayAmt(object.getGtUsedPayAmt());//担保二手车-未放款支付表敞口
            dbOldCar.setGtUsedPaidAmt(object.getGtUsedPaidAmt());//担保二手车-已放款支付表敞口

            //汇总
            CapPrjArInfoVO total = new CapPrjArInfoVO();
            total.setRecordType(1);
            total.setRzOrDb(2);
            total.setCarType(2);
            total.setId(object.getId());
            total.setCrtUsrId(object.getCrtUsrId());
            total.setPrjId(object.getPrjId());//项目ID
            total.setMainId(object.getMainId());//主体ID(承租人/担保人/挂靠公司/供应商ID)
            total.setMainNm(object.getMainNm());//主体名称(承租人/担保人/挂靠公司/供应商名称)
            total.setMainTypCd(object.getMainTypCd());//主体类型(CAP0053)：1.承租人 2.担保人 3.挂靠公司 4.供应商
            total.setTotalOvdHisCnt(object.getFlNewOvdHisCnt() + object.getFlUsedOvdHisCnt() + object.getGtNewOvdHisCnt() + object.getGtUsedOvdHisCnt());
            total.setTotalOvdCnt(object.getFlNewOvdCnt() + object.getFlUsedOvdCnt() + object.getGtNewOvdCnt() + object.getGtUsedOvdCnt());
            total.setGtTotalPrjAudAmt(object.getFlNewPrjAudAmt() + object.getFlUsedPrjAudAmt() + object.getGtNewPrjAudAmt() + object.getGtUsedPrjAudAmt());
            total.setGtTotalPayAmt(object.getFlNewPayAmt() + object.getFlUsedPayAmt() + object.getGtNewPayAmt() + object.getGtUsedPayAmt());
            total.setGtTotalPaidAmt(object.getFlNewPaidAmt() + object.getFlUsedPaidAmt() + object.getGtNewPaidAmt() + object.getGtUsedPaidAmt());



            list.add(total);
            list.add(rzNewCar);
            list.add(rzOldCar);
            list.add(dbNewCar);
            list.add(dbOldCar);
        }

        return list;
    }

}
